package com.viphuli.app.tool.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.LogUtils;
import com.offroader.utils.ViewUtils;
import com.umeng.message.ALIAS_TYPE;
import com.umeng.message.PushAgent;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.part.AccountUser;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.common.MyPageHelper;
import com.viphuli.app.tool.handler.MyBaseHttpResponseHandler.HandlerType;
import com.viphuli.app.tool.http.ApiRequest;

/**
 * 我的
 * 
 * @author li.li
 *
 */
public class AccountFragment extends BaseProgressFragment {

	@Bind(R.id.id_account_portrait)
	protected ImageView portrait;

	@Bind(R.id.id_account_name)
	protected TextView name;

	@Bind(R.id.id_account_phone)
	protected TextView phone;

	@Bind(R.id.id_account_wechat)
	protected TextView wechat;

	@Bind(R.id.id_account_user_type)
	protected TextView userType;

	@Bind(R.id.id_account_type_btn)
	protected View typeBtn;

	@Bind(R.id.id_account_remind_layout)
	protected View remindLayout;

	@Bind(R.id.id_account_bind_wechat_layout)
	protected View wechatLayout;
	
	@Bind(R.id.id_account_feedback)
	protected View feedback;
	
	@Bind(R.id.id_account_manual)
	protected View manual;

	@Bind(R.id.id_account_logout)
	protected View logout;

	private AccountUser user;

	private String uid;

	protected int obtainContentRes() {
		return R.layout.frag_account;
	}

	@Override
	protected void initParams() {

		Bundle args = getArguments();
		uid = args.getString("uid");
	}

	@Override
	protected void initView() {

	}

	@Override
	public void onStart() {
		super.onStart();

		initRequest(HandlerType.loading);
	}

	@Override
	protected void initData() {

		initRequest(HandlerType.loading);

	}

	@Override
	protected void initRequest(HandlerType handlerType) {
		super.initRequest(handlerType);

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {

			if (!tokenKeeper.getOpenId().equals(uid)) {

				userType.setVisibility(View.GONE);
				typeBtn.setVisibility(View.GONE);
				remindLayout.setVisibility(View.GONE);
				wechatLayout.setVisibility(View.GONE);
				logout.setVisibility(View.GONE);
				feedback.setVisibility(View.GONE);
				manual.setVisibility(View.GONE);

			}

			RequestParams params = new RequestParams();
			params.put("uid", uid);
			ApiRequest.account.request(this, params, handlerType);
		}
	}

	@OnClick(R.id.id_account_logout)
	public void logout() {

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {

			//移除友盟推送别名
			PushAgent mPushAgent = PushAgent.getInstance(getActivity());

			try {
				mPushAgent.removeAlias(tokenKeeper.getUser().getUid(), ALIAS_TYPE.WEIXIN);
			} catch (Throwable e) {
				LogUtils.error(e.getMessage(), e);
			}

		}

		AccessTokenKeeper.clear(getActivity());
		initRequest(HandlerType.waitting);
		ViewUtils.toast(R.string.tip_logout_success);

		caller.finish();
	}

	@OnClick(R.id.id_account_remind_layout)
	public void remind() {
		AccountRemindFragment.go(getActivity());
	}

	@OnClick(R.id.id_account_type_btn)
	public void userType() {
		AccountTypeDialogFragment accountTypeDialogFrag = new AccountTypeDialogFragment();

		Bundle args = new Bundle();
		args.putString("user_name", name.getText().toString());
		args.putString("user_type_name", userType.getText().toString());
		accountTypeDialogFrag.setArguments(args);

		accountTypeDialogFrag.show(getChildFragmentManager(), accountTypeDialogFrag.getClass().getSimpleName());
	}

	@OnClick(R.id.id_account_user_name)
	public void clickUserName() {

		String title = "姓名";
		String content = user.getUserName();
		int userType = user.getUserType();
		String uid = user.getUid();
		UpdateUserNameFragment.go(getActivity(), title, content, userType, uid);

	}
	
	@OnClick(R.id.id_account_feedback)
	public void clickFeedback() {
		
		AccountFeedbackFragment.go(getActivity());

	}
	
	@OnClick(R.id.id_account_manual)
	public void clickManual() {
		String title = "使用手册";
		String url = "http://v.qq.com/page/s/4/v/s01827d274v.html";

		MyPageHelper.webView.showMyWebViewPage(getActivity(), url, title);
	}

	public ImageView getPortrait() {
		return portrait;
	}

	public TextView getName() {
		return name;
	}

	public TextView getPhone() {
		return phone;
	}

	public TextView getWechat() {
		return wechat;
	}

	public TextView getUserType() {
		return userType;
	}

	public void setUser(AccountUser user) {
		this.user = user;
	}

	public static void go(Activity act, String uid) {

		Bundle args = new Bundle();
		args.putString("uid", uid);

		MyPageHelper.account.showMyPage(act, args);
	}

}
